package com.frogs.ucenter.v1.request;

import javax.servlet.http.HttpServletRequest;

import org.owasp.esapi.ESAPI;

import net.sf.json.JSONObject;

import com.alibaba.fastjson.JSONArray;
import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.util.UserMessageTypeEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.exception.RequestIllegalException;

public class DirectMessageSendRequest extends Request {
	
	private static final long serialVersionUID = 1029112668063469554L;
	/**
	 * 发送人
	 */
	private String sender;
	/**
	 * 接收人
	 */
	private JSONArray receiverArray = new JSONArray();
	
	private String senderId;
	
	private Integer receiverType;
	
	//发送时间(定时发送使用，为空表示实时发送)
	private String sendTime;
	
	//为空表示永远保留
	private String overTime;
	
	//发送类型（系统下发/后台下发/第三方下发）
	private Integer sendType;
	
	private Integer applicableId;
	
	private String subject;
	
	/**
	 * 短信内容
	 */
	private String content;
	
	/** */
	public DirectMessageSendRequest() {
	}
	
	@Override
	public void parse(HttpServletRequest request) {
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
	
		this.sender = requestInfo.getParameter(ApiUrlParamEnum.sender.getParamName());
		this.senderId = requestInfo.getParameter(ApiUrlParamEnum.senderId.getParamName());
		String receiver = requestInfo.getParameter(ApiUrlParamEnum.receiver.getParamName(), "");
		
		this.receiverType = requestInfo.getIntParameter(ApiUrlParamEnum.receiverType.getParamName(), UserMessageTypeEnum.RECEIVER_PERSONAL.getId());
		this.subject = requestInfo.getParameter(ApiUrlParamEnum.subject.getParamName());
		this.sendTime = requestInfo.getParameter(ApiUrlParamEnum.sendTime.getParamName(), "");
		this.overTime = requestInfo.getParameter(ApiUrlParamEnum.overTime.getParamName(), "");
		this.sendType = requestInfo.getIntParameter(ApiUrlParamEnum.sendDmType.getParamName(), UserMessageTypeEnum.SEND_SYSTEM.getId());
		this.applicableId = requestInfo.getIntAttribute(ApiUrlParamEnum.applicablePlatformId.getParamName(), ApplicablePlatformEnum.WEB.getId());
		
		this.content = StringUtil.fixHtml(requestInfo.getParameter(ApiUrlParamEnum.content.getParamName()));

		if ( StringUtil.isEmpty(sender) && StringUtil.isEmpty(senderId) ) {
			throw new RequestIllegalException("发送人或发送人ID不能为空");
		}
		
		if(receiverType != UserMessageTypeEnum.RECEIVER_ALL.getId()) {
			try {
				if(!StringUtil.isEmpty(receiver)) {
					this.receiverArray = JSONArray.parseArray(receiver);
				} else {
					throw new RequestIllegalException("收件人不能为空");
				}
				
				if(null == receiverArray || receiverArray.size() == 0) {
					throw new RequestIllegalException("收件人列表不能为空");
				}
			} catch(Exception e) {
				throw new RequestIllegalException("收件人数据格式错误");
			}
		} else {
			JSONObject receiverObj = new JSONObject();
			receiverObj.put("receiver", "");
			receiverObj.put("receiverId", "-1");
			receiverArray.add(receiverObj);
		}
		
		if ( StringUtil.isEmpty(content) ) {
			throw new RequestIllegalException("error content: " + content);
		}
		
		if ( StringUtil.isEmpty(subject) ) {
			throw new RequestIllegalException("error subject: " + subject);
		}
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getSenderId() {
		return senderId;
	}

	public void setSenderId(String senderId) {
		this.senderId = senderId;
	}

	public Integer getReceiverType() {
		return receiverType;
	}

	public void setReceiverType(Integer receiverType) {
		this.receiverType = receiverType;
	}

	public String getSendTime() {
		return sendTime;
	}

	public void setSendTime(String sendTime) {
		this.sendTime = sendTime;
	}

	public String getOverTime() {
		return overTime;
	}

	public void setOverTime(String overTime) {
		this.overTime = overTime;
	}

	public Integer getSendType() {
		return sendType;
	}

	public void setSendType(Integer sendType) {
		this.sendType = sendType;
	}

	public Integer getApplicableId() {
		return applicableId;
	}

	public void setApplicableId(Integer applicableId) {
		this.applicableId = applicableId;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public JSONArray getReceiverArray() {
		return receiverArray;
	}

	public void setReceiverArray(JSONArray receiverArray) {
		this.receiverArray = receiverArray;
	}

}
